var app = getApp()
const util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    customBar: app.globalData.customBar,
    API_BASE_URL: util.API_BASE_URL,
    popupTripInfoShow: false,
    dayIndex: 0,
    billid: '',
    info: {},
    tripDetail: [],
    targetId: '',
    offsets: [0],      // 元素顶部位置数组
    isbtn: 1,//按钮权限
    zydList: []
  },
  //图片预览
  previewPic(e) {
    let xltp = this.data.info.xltp;
    wx.previewImage({ urls: [`${util.API_BASE_URL}itf/common/att/download.do?attrId=${xltp}`] })
  },
  previewImage: function (e) {
    const tps = e.currentTarget.dataset.tps; // 获取当前图片地址
    const currentUrl = e.currentTarget.dataset.src; // 获取当前图片地址
    let imgList = [];
    tps.forEach(item => {
      imgList.push(this.data.API_BASE_URL + 'itf/common/att/download.do?attrId=' + item)
    })
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: imgList,      // 需要预览的图片http链接列表
      success: (res) => { },
      fail: (err) => {
        console.error('预览失败', err)
      }
    })
  },
  //
  handleScroll(e) {
    const scrollTop = e.detail.scrollTop;
    const offsets = this.data.offsets;
    let dayIndex = 0;
    // 从后向前查找第一个小于等于 scrollTop 的位置
    for (let i = offsets.length - 1; i >= 0; i--) {
      if (offsets[i] <= scrollTop) {
        dayIndex = i;
        break;
      }
    }
    this.setData({ dayIndex });
  },
  // 点击按钮滚动到第5项（索引为4）
  scrollToItem(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      dayIndex: index,
      targetId: 'item' + index // 对应 id="item4" 的元素
    });
  },
  //前往资源点详情
  goZydDetail(e) {
    const zyd_pk = e.currentTarget.dataset.zyd_pk;
    wx.navigateTo({
      url: '/pages/pointDetails/pointDetails?billid=' + zyd_pk,
    })
  },
  async queryRouteDetail() {
    const _this = this;
    const billid = this.data.billid
    console.log(_this.data.zydParams)
    return util.request('/itf/common/hongpei/resource/queryRouteDetail.json', {
      billid
    }).then(res => {
      if (res.success) {
        let data = res.data;
        let detailList = res.data.detailList;
        let banner = [];//处理头部banner
        if (data.fmtp) {
          let imgArr = data.fmtp.split(",");
          if (imgArr && imgArr.length > 0) {
            imgArr.forEach(item => {
              banner.push(util.API_BASE_URL + 'itf/common/att/download.do?attrId=' + item);
            })
          }
        }

        if (detailList) {
          detailList.forEach(item => {
            item.tps = item.tps ? item.tps.split(",") : item.tps;
          })
          _this.setData({
            'info.banner': banner,
            'info.title': data.bt,
            'info.slot': data.xlxq_memo,
            'info.day': data.xxts,
            'info.reason': data.tjly,
            'info.ckfy': data.ckfy,
            'info.xltp': data.xltp,
            tripDetail: detailList,
            zydList: _this.data.zydParams[data.billid]
          })
        }
      } else {
        util.showMsg({ msg: res.message || res.msg });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const { billid, isbtn } = options;
    const zydParams = {
      '32be462f09da414b94d174bb7aa43914': [
        {
          billid: '956648b2971b44ea956c8197d64ba658',
          title: '古田会议会址旧址群',
          width: 216,
          height: 36,
          top: 186,
          left: 422
        }, {
          billid: '11b6bb76506446a58fcd414e62ebf699',
          title: '新泉整训旧址群',
          width: 216,
          height: 36,
          top: 140,
          left: 382
        }, {
          billid: '4b8d7f72cd9744379c1da598de017cc4',
          title: '毛泽东才溪乡调查旧址群',
          width: 244,
          height: 36,
          top: 192,
          left: 30
        }, {
          billid: '17cef1adf9a644499eb6a9168e25cc4c',
          title: '松毛岭战斗烈士纪念碑',
          width: 230,
          height: 36,
          top: 100,
          left: 350
        }, {
          billid: '3fcc3bd514cd45eeabfbcebb35b3c859',
          title: '红军长征第一村中复村',
          width: 230,
          height: 36,
          top: 56,
          left: 336
        }, {
          billid: 'bd35b4ebba72417bbf3170f70ca907c8',
          title: '长汀苏维埃政府旧址群',
          width: 226,
          height: 36,
          top: 28,
          left: 20
        }, {
          billid: '3f4928970f2347cd94ece08849775feb',
          title: '中央红色交通线',
          width: 170,
          height: 36,
          top: 350,
          left: 170
        }, {
          billid: 'bc8cf6ab03fa4099a47b7865954d9a9a',
          title: '永定万源楼',
          width: 140,
          height: 36,
          top: 292,
          left: 430
        }
      ],
      '59ab148e6dc74bf5b11e9c71b1ffb87e': [
        {
          billid: '956648b2971b44ea956c8197d64ba658',
          title: '古田会议会址旧址群',
          width: 212,
          height: 36,
          top: 236,
          left: 424
        }, {
          billid: '11b6bb76506446a58fcd414e62ebf699',
          title: '新泉整训旧址群',
          width: 176,
          height: 36,
          top: 190,
          left: 376
        }, {
          billid: '17cef1adf9a644499eb6a9168e25cc4c',
          title: '松毛岭战斗烈士纪念碑',
          width: 228,
          height: 36,
          top: 146,
          left: 348
        }, {
          billid: '3fcc3bd514cd45eeabfbcebb35b3c859',
          title: '红军长征第一村中复村',
          width: 228,
          height: 36,
          top: 80,
          left: 330
        }, {
          billid: 'bd35b4ebba72417bbf3170f70ca907c8',
          title: '长汀苏维埃政府旧址群',
          width: 228,
          height: 36,
          top: 70,
          left: 16
        }, {
          billid: 'bfa194d38f144131b032e3253ff486f4',
          title: '红旗跃过汀江渡口',
          width: 190,
          height: 36,
          top: 160,
          left: 110
        }
      ],
      '884f1a501d8549f98e5d2750a26c408a': [
        {
          billid: '956648b2971b44ea956c8197d64ba658',
          title: '古田会议会址旧址群',
          width: 200,
          height: 36,
          top: 194,
          left: 448
        }, {
          billid: '6769431a48f84ab4963d51331efcd4f2',
          title: '梅花山自然保护区',
          width: 190,
          height: 36,
          top: 56,
          left: 460
        }, {
          billid: '8302f3af231844a7ba86b45b940c65bd',
          title: '长汀县水土保持科教园',
          width: 230,
          height: 36,
          top: 58,
          left: 84
        }, {
          billid: '362e440a90ff43f1ab97c098cdda21a0',
          title: '武平县万安镇捷文村',
          width: 210,
          height: 36,
          top: 198,
          left: 10
        }, {
          billid: 'db47ed340fcd4139b2c393741453c016',
          title: '城厢镇云寨村',
          width: 150,
          height: 36,
          top: 166,
          left: 266
        }, {
          billid: '8ab75d583f1d441cb2b2b16ce8f62318',
          title: '城厢镇尧禄村',
          width: 150,
          height: 36,
          top: 262,
          left: 120
        }, {
          billid: 'f8c62deb82824894b759ccccefa145ee',
          title: '福建龙岩国家现代林业科技示范园',
          width: 194,
          height: 52,
          top: 274,
          left: 284
        }, {
          billid: 'beb2e688ec544d84bfe89954bde2010c',
          title: '大陆阿里山',
          width: 136,
          height: 36,
          top: 294,
          left: 532
        }
      ],
      '9aef453023104742896f8595124c4dd6': [
        {
          billid: 'a2d517d608d446c78403c5714f2b1fb7',
          title: '振成楼',
          width: 182,
          height: 50,
          top: 348,
          left: 408
        }, {
          billid: '205e4253d215403d8138a9fae0ade267',
          title: '培田古村落“耕读传家”传统文化教育示范点',
          width: 244,
          height: 56,
          top: 44,
          left: 60
        }, {
          billid: 'f08a8f700db9497b9dbdb21d8ba28138',
          title: '上杭传统文化街区',
          width: 192,
          height: 50,
          top: 140,
          left: 124
        }, {
          billid: 'a8bb954dde8049ba999ec7a6ab683973',
          title: '兴贤坊传统文化街区',
          width: 256,
          height: 50,
          top: 256,
          left: 10
        }, {
          billid: '4653b0f026fd44028af4cd2fe9e1abb8',
          title: '王景弘史迹陈列馆',
          width: 140,
          height: 56,
          top: 40,
          left: 516
        }
      ]
    }
    this.setData({ billid, isbtn, zydParams });
    await this.queryRouteDetail();
    // 等待视图层渲染完成
    wx.nextTick(() => {
      this.calculateOffsets();
    });
  },
  //返回
  back() {
    wx.navigateBack({
      delta: 1 // 返回的页面层数，1 表示返回上一页
    });
  },
  //生成行程单
  createBtn() {
    const { billid } = this.data;
    this.setData({ popupTripInfoShow: true, billid })

    // 获取子组件实例
    const child = this.selectComponent('#popupTripinfo');
    // 调用子组件方法
    if (child) {
      child.init();
    }
  },
  toBack(e) {
    wx.navigateBack()
  },
  // 计算所有元素的顶部位置
  calculateOffsets() {
    const query = wx.createSelectorQuery().in(this);

    query.selectAll('.planInfo').boundingClientRect(rects => {
      if (!rects || rects.length === 0) {
        console.error('未找到 .planInfo 元素');
        return;
      }

      console.log('获取到的元素信息:', rects);
      const offsets = [0];
      let totalHeight = 0;

      for (let i = 1; i < rects.length; i++) {
        totalHeight += rects[i - 1].height;
        offsets.push(totalHeight);
      }
      this.setData({ offsets });
    }).exec();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})